﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace LiuYan.Base
{
    public class VerificationCode
    {
        #region Fields

        /// <summary>
        ///     背景颜色
        /// </summary>
        public Color BackgroundColor = Color.FromArgb(51, 153, 204);

        /// <summary>
        ///     验证码字体颜色
        /// </summary>
        public Color FontColor = Color.White;

        /// <summary>
        ///     字体大小
        /// </summary>
        public int FontSize = 20;

        /// <summary>
        ///     图片高度
        /// </summary>
        public int Height = 35;

        /// <summary>
        ///     验证码字符串长度
        /// </summary>
        public int IntLength = 5;

        /// <summary>
        ///     是否有干扰线
        /// </summary>
        public bool IsLines = false;

        /// <summary>
        ///     干扰线颜色
        /// </summary>
        public Color LinesColor = Color.WhiteSmoke;

        /// <summary>
        ///     干扰线粗细
        /// </summary>
        public int LinesWeight = 1;

        /// <summary>
        ///     字符间隔间距
        /// </summary>
        public int TextSplitWidth = 12;

        /// <summary>
        ///     字体上边距范围
        /// </summary>
        public int TextTopHeight = 10;

        /// <summary>
        ///     图片宽度
        /// </summary>
        public int Width = 70;

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 绘制验证码
        /// </summary>
        /// <param name="verif">
        /// 验证码
        /// </param>
        /// <returns>
        /// The <see cref="Bitmap"/>.
        /// </returns>
        public Bitmap DrawImage(out string verif)
        {
            var bmp = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(new SolidBrush(this.BackgroundColor), 0, 0, this.Width, this.Height);
            var font = new Font(FontFamily.GenericSerif, this.FontSize, FontStyle.Bold, GraphicsUnit.Pixel);
            var r = new Random();

            // 合法随机显示字符列表
            string strLetters = "1234567890";
            var sb = new StringBuilder();

            // 将随机生成的字符串绘制到图片上
            for (int i = 0; i < this.IntLength; i++)
            {
                sb.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
                g.DrawString(
                    sb[sb.Length - 1].ToString(),
                    font,
                    new SolidBrush(this.FontColor),
                    i * this.TextSplitWidth,
                    r.Next(0, this.TextTopHeight));
            }

            // 生成干扰线条
            if (this.IsLines)
            {
                var pen = new Pen(new SolidBrush(this.LinesColor), this.LinesWeight);
                for (int i = 0; i < 10; i++)
                {
                    g.DrawLine(
                        pen,
                        new Point(r.Next(0, this.Width - 1), r.Next(0, this.Height - 1)),
                        new Point(r.Next(0, this.Width - 1), r.Next(0, this.Height - 1)));
                }
            }

            verif = sb.ToString();
            return bmp;
        }

        #endregion
    }
}
